package com.zhuss.core.auth;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.unit.core.company.CompanyBean;
import com.zhuss.bean.unit.core.user.UserBean;
import com.zhuss.bean.vo.core.company.DetailCompanyVo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.AbstractBo;
import com.zhuss.common.AbstractVo;
import com.zhuss.core.processor.CompanyRead;
import com.zhuss.core.processor.UserRead;

@Aspect
@Component
public class UserInfoAspect {
	
	
	@Autowired
	@Qualifier("userRead")
	private UserRead userRead;
	
	@Autowired
	@Qualifier("companyRead")
	private CompanyRead companyRead;
	
	
	@Pointcut("execution(* com.zhuss.controller.*.*.*(..))")
	public void pointCut() {
		
	}
	
	@Before("pointCut()")
	public void around(JoinPoint point) throws Throwable {
		String targetName = point.getTarget().getClass().getName();
        String methodName = point.getSignature().getName();
        Object[] arguments = point.getArgs();
        if (arguments.length <= 0) {
        	return;
        }
        Object obj = arguments[0];
        Class<?> targetClass = Class.forName(targetName);
        Method[] methodList = targetClass.getDeclaredMethods();
        for (Method m : methodList) {
    		if (obj instanceof AbstractBo && methodName.equals(m.getName())) {
    			AbstractBo bo = (AbstractBo) obj;
    			Long userId = bo.getConcurrentUserId();
    			if (userId != null && userId > 0l) {
    				DetailUserVo user = (DetailUserVo) userRead.detail(userId);
    				Long companyId = user.getCompanyId();
    				UserBean concurrentUser = new UserBean();
    				BeanUtils.copyProperties(user, concurrentUser);;
    				bo.setConcurrentUser(concurrentUser);
    				if (companyId != null && companyId != 0l) {
    					DetailCompanyVo company = (DetailCompanyVo) companyRead.detail(companyId);
        				CompanyBean concurrentCompany = new CompanyBean();
        				BeanUtils.copyProperties(company, concurrentCompany);
        				bo.setConcurrentCompany(concurrentCompany);
    				}
    				
    				break;
    			}
    		}
        }
    }
	
	
	@AfterReturning(pointcut="pointCut()", returning="result")
	public void after(JoinPoint point, Object result) throws Throwable {
		Object[] arguments = point.getArgs();
        if (arguments.length <= 0) {
        	return;
        }
		Object obj = arguments[0];
		if (obj instanceof AbstractBo) {
			AbstractBo bo = (AbstractBo) obj;
			bo.getConcurrentUser();
			if (result instanceof AbstractVo) {
				AbstractVo vo = ((AbstractVo) result);
				vo.setConcurrentUser(bo.getConcurrentUser());
			}
		}
    }
}
